<?php

namespace app\api\model\question;

use app\common\model\questionnaire\Topic as Model;

class Topic extends Model
{
    public function getList($questionnaire_id)
    {
        $where = [
            'questionnaire_id' => $questionnaire_id,
            'is_delete' => 0,
        ];

        // 随机50题
        return $this
            ->field('topic_id,topic_type,items,title')
            ->where($where)
            ->orderRand()
            ->limit(50)
//            ->order('topic_type', 'asc')
            ->select();
    }

    public function getPracticeList($questionnaire_id)
    {
        $where = [
            'questionnaire_id' => $questionnaire_id,
            'is_delete' => 0,
        ];
        return $this
            ->field('topic_id,topic_type,items,title,correct_answer,analysis')
            ->where($where)
            ->order('sort', 'asc')
            ->select();
    }


    public function byTypeCount($question_id, $qt_type)
    {
        return $this->where([
            'qt_type' => $qt_type,
            'question_id' => $question_id,
            'is_delete' => 0,
            'status' => 0,
        ])->count();
    }

    public function byTypeRandList($question_id, $qt_type, $limit)
    {
        $where = [
            'question_id' => $question_id,
            'is_delete' => 0,
            'qt_type' => $qt_type,
            'status' => 0,
        ];
        return $this->where($where)->limit($limit)->orderRaw('rand()')->select()->toArray();
    }

}
